package com.ktkq.action.teacher;



import java.io.File;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.stereotype.Controller;

import com.ktkq.action.BaseAction;
import com.ktkq.service.teacher.TeacherService;
import com.ktkq.util.PageFinder;
import com.ktkq.util.Pageing;
import com.ktkq.util.StringUtil;
import com.ktkq.vo.Course;
import com.ktkq.vo.Teacher;


@SuppressWarnings("serial")
@Controller
public class TeacherAction extends BaseAction implements SessionAware{
	
	private Pageing pageing;
	
	private PageFinder<Teacher> pageFinder;
	
	 private List<File> file;
	 private List<String> fileFileName;
	 private List<String> fileContentType;
  
	Map session;
	 @Resource(name="teacherService") 
	 TeacherService teacherService;
	
	 Teacher teacher;
	
	int teacherId;

	int[] deleteList;  //批量删除
    String resultView;    //结果视图    
	@Override
	public String execute() throws Exception {
		if(pageFinder==null){
			pageFinder=new PageFinder<Teacher>();
		}

		System.out.println(teacherService.queryListById(1));
		
		return INPUT;
	}
	/**
	 * 查询分页信息
	 * @return
	 */
	public String findTeacherList(){
		if(pageing==null){
			pageing=new Pageing();
		}
		if(pageFinder==null){
			pageFinder=new PageFinder<Teacher>();
		}
		int page=pageFinder.getPageNo();
		pageFinder.setPageSize(20);
		pageing.setPageSize(20);
		pageing.setStartOfPage(pageFinder.getStartOfPage());
		//分页信息
		pageFinder=teacherService.queryList(pageing);
		pageFinder.setPageNo(page);
		pageFinder.setPageSize(20);
		return SUCCESS;
	}
	/**
	 * 通过ID查询信息
	 * @return
	 */
	public String findTeacherById(){
		if(pageFinder==null){
			pageFinder=new PageFinder<Teacher>();
		}
		
		pageFinder.setDataList(teacherService.queryListById(teacherId));
		if(session.get("teacherID")!=null){
			if(teacher!=null){
				resultView = "/main/Pass.jsp";
			}else{
				resultView = "/main/teacherInfo.jsp";
			}
			
		}else{
			resultView = "/admin/teacher/editTeacher.jsp";
		}
			return "edit";
	}
	/**
	 * 添加信息
	 * @return
	 */
	public String addTeacher(){
//		if(student !=null){
//		studentService.insert(student);
//		}
		if(pageFinder==null){
			pageFinder=new PageFinder<Teacher>();
		}
		String uploadFileName  = "studentFile."+StringUtil.getExtention(fileFileName.get(0));

		try {
		if (file != null) {
			
            File savefile = new File(new File(getSavePath()),uploadFileName);
            if (!savefile.getParentFile().exists())
                savefile.getParentFile().mkdirs();
            FileUtils.copyFile(file.get(0), savefile);
			
		}
		} catch (Exception e) {
			  ServletActionContext.getRequest().setAttribute("message","添加失败");
         System.out.println("失败");
		}
		 pageFinder.setDataList(teacherService.UploadExcel(getSavePath() + "\\" +uploadFileName,teacher));
			if(pageFinder.getDataList().size()==0){
				  System.out.println("该表没有学生信息");	
				  ServletActionContext.getRequest().setAttribute("message","该表没有学生信息");
			}
		return INPUT;
	}
	
	/**
	 * 修改信息
	 * @return
	 */
	public String updateTeacher(){
		if(teacher !=null){
			teacherService.update(teacher);
		}
		if(session.get("teacherID")!=null){
			if(pageing!=null){
				resultView = "/admin/teacher_findTeacherById.action?teacherId="+session.get("teacherID")+"&teacher.id=0";
			}else{
				resultView = "/admin/teacher_findTeacherById.action?teacherId="+session.get("teacherID");
			}
			
		}else{
			resultView = "/admin/teacher_findTeacherList.action?pageFinder.pageNo=1";
		}
		return "update";
	}
	
	/**
	 * 删除信息
	 * @return
	 */
	public String deleteTeacher(){
		if(deleteList!=null){
		      for(int id:deleteList){
		    	  Teacher stu =  new Teacher();
		    	  stu.setId(id);
		    	  teacherService.delete(stu);
		      }
			}else if(teacher !=null){
			teacherService.delete(teacher);
		}
		resultView = "/admin/teacher_findTeacherList.action?pageFinder.pageNo=1";
		return "update";
	}
	
	public String queryTeacher(){
		
		pageFinder.setDataList(teacherService.queryListById(teacherId));
		
		resultView = "/admin/teacher_findTeacherList.action?pageFinder.pageNo=1";
		return "update";
	}
	
	
	public Pageing getPageing() {
		return pageing;
	}
	
	
	 public Map getSession() {
		return session;
	}
	public void setSession(Map session) {
		this.session = session;
	}
	public PageFinder<Teacher> getPageFinder() {
		return pageFinder;
	}
	public void setPageFinder(PageFinder<Teacher> pageFinder) {
		this.pageFinder = pageFinder;
	}
	public TeacherService getTeacherService() {
		return teacherService;
	}
	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public int getTeacherId() {
		return teacherId;
	}
	public void setTeacherId(int teacherId) {
		this.teacherId = teacherId;
	}
	public void setPageing(Pageing pageing) {
		this.pageing = pageing;
	}
	/**
     * 返回上传文件的保存位置
     * 
     * @return
     */
    public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath("/studentFile"); 
    }

 
	public List<File> getFile() {
		return file;
	}
	public void setFile(List<File> file) {
		this.file = file;
	}
	public List<String> getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}
	public List<String> getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
	public int[] getDeleteList() {
		return deleteList;
	}
	public void setDeleteList(int[] deleteList) {
		this.deleteList = deleteList;
	}
	public String getResultView() {
		return resultView;
	}
	public void setResultView(String resultView) {
		this.resultView = resultView;
	}

	

	

  
	

}
